package com.crm.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.crm.domain.*;
import com.crm.crm.domain.dto.report.ContractStatisticsListDto;
import com.crm.crm.domain.vo.BackLogVo;
import com.crm.crm.domain.vo.IndexTodayDataVo;
import com.crm.crm.domain.vo.ReportFunnelVo;
import com.crm.crm.domain.vo.business.StatisticVo;
import com.crm.crm.mapper.IndexMapper;
import com.crm.crm.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class IndexServiceImpl extends ServiceImpl<IndexMapper,InsertRecord> implements IndexService {
    @Autowired
    private IndexMapper indexMapper;

    @Override
    public IndexTodayDataVo getTodayInfo() {
        IndexTodayDataVo indexTodayDataVo = new IndexTodayDataVo();
        // 获取今天的开始时间和结束时间
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        log.info("今天的开始时间todayStart: " + todayStart);
        log.info("今天的结束时间todayEnd: " + todayEnd);
        LambdaQueryWrapper<Clue> clueWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<CluePool> cluePoolWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<BusinessOpportunity> businessWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Contract> contractWrapper = new LambdaQueryWrapper<>();
        clueWrapper.between(Clue::getCreateTime, todayStart, todayEnd);
        businessWrapper.between(BusinessOpportunity::getCreateTime, todayStart, todayEnd);
        contractWrapper.between(Contract::getCreateTime, todayStart, todayEnd);
        cluePoolWrapper.between(CluePool::getCreateTime, todayStart, todayEnd);
        //今日新增线索
        Integer clueNum = indexMapper.countByWrapper(clueWrapper, "clue");
        Integer cluePoolNum = indexMapper.countByWrapper(cluePoolWrapper, "clue_pool");
        Integer clueNums = clueNum + cluePoolNum;
        indexTodayDataVo.setTodayCluesNum(clueNums);
        //今日新增商机
        Integer businessNum = indexMapper.countByWrapper(businessWrapper,"business_opportunity");
        indexTodayDataVo.setTodayBusinessNum(businessNum);
        //今日新增客户
        Integer contractNum = indexMapper.countByWrapper(contractWrapper, "contract");
        indexTodayDataVo.setTodayContractNum(contractNum);
        //今日销售额
        indexTodayDataVo.setTodaySalesAmount(null);
        log.info("首页数据{}",indexTodayDataVo);
        return indexTodayDataVo;
    }

    @Override
    public List<StatisticVo> businessChangeStatistics(ContractStatisticsListDto dto) {
        String beginCreateTime = dto.getBeginCreateTime();
        String endCreateTime = dto.getEndCreateTime();
        log.info("开始时间{}",beginCreateTime);
        log.info("结束时间{}",endCreateTime);
        LambdaQueryWrapper<InsertRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(InsertRecord::getCreateTime, beginCreateTime, endCreateTime);
        List<InsertRecord> recordList = indexMapper.selectList(wrapper);
        log.info("查询结果{}",recordList);
        ArrayList<StatisticVo> statisticVos = new ArrayList<>();
        for (InsertRecord insertRecord : recordList) {
            StatisticVo statisticVo = new StatisticVo();
            statisticVo.setCreate_by(insertRecord.getCreateBy());
            statisticVo.setDeptName(insertRecord.getDeptName());
            Integer businessChangeCount = insertRecord.getBusinessChangeCount();
            Integer businessCount = insertRecord.getBusinessCount();
            int radio = 0;
            if (businessCount != null && businessCount != 0) { // 避免除以0
                radio = businessChangeCount * 100 / businessCount;
            }
            statisticVo.setRadio(radio);
            statisticVo.setNum(businessChangeCount);
            statisticVos.add(statisticVo);
        }
        statisticVos.sort(
                Comparator
                        .comparingInt(StatisticVo::getNum).reversed()
                        .thenComparing(Comparator.comparingInt(StatisticVo::getRadio).reversed())
        );
        return statisticVos;
    }

    @Override
    public List<StatisticVo> salesStatistic(ContractStatisticsListDto dto) {
        String beginCreateTime = dto.getBeginCreateTime();
        String endCreateTime = dto.getEndCreateTime();
        log.info("开始时间{}",beginCreateTime);
        log.info("结束时间{}",endCreateTime);
        LambdaQueryWrapper<InsertRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(InsertRecord::getCreateTime, beginCreateTime, endCreateTime);
        List<InsertRecord> recordList = indexMapper.selectList(wrapper);
        log.info("查询结果{}",recordList);
        ArrayList<StatisticVo> statisticVos = new ArrayList<>();
        for (InsertRecord insertRecord : recordList) {
            StatisticVo statisticVo = new StatisticVo();
            statisticVo.setCreate_by(insertRecord.getCreateBy());
            statisticVo.setDeptName(insertRecord.getDeptName());
            Integer clueChangeCount = insertRecord.getClueChangeCount();
            Integer clueCount = insertRecord.getClueCount();
            int radio = 0;
            if (clueCount != null && clueCount != 0) { // 避免除以0
                radio = clueChangeCount * 100 / clueCount;
            }
            statisticVo.setRadio(radio);
            statisticVo.setNum(clueChangeCount);
            statisticVos.add(statisticVo);
        }
        statisticVos.sort(
                Comparator
                        .comparingInt(StatisticVo::getNum).reversed()
                        .thenComparing(Comparator.comparingInt(StatisticVo::getRadio).reversed())
        );
        return statisticVos;
    }

    @Override
    public BackLogVo getBackLog(ContractStatisticsListDto dto) {
        String beginCreateTime = dto.getBeginCreateTime();
        String endCreateTime = dto.getEndCreateTime();
        log.info("开始时间{}",beginCreateTime);
        log.info("结束时间{}",endCreateTime);
        LambdaQueryWrapper<Clue> clueWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<BusinessOpportunity> businessWrapper = new LambdaQueryWrapper<>();
        //待分配线索
        clueWrapper.between(Clue::getCreateTime, beginCreateTime, endCreateTime);
        Integer clueNum = indexMapper.countByWrapper(clueWrapper, "clue");
        //待跟进线索
        clueWrapper.eq(Clue::getStatus, 1);
        Integer clueNumTo = indexMapper.countByWrapper(clueWrapper, "clue");
        //待分配商机
        businessWrapper.between(BusinessOpportunity::getCreateTime, beginCreateTime, endCreateTime);
        Integer businessNum = indexMapper.countByWrapper(businessWrapper,"business_opportunity");
        //待跟进商机
        businessWrapper.eq(BusinessOpportunity::getStatus, 1);
        Integer businessNumTo = indexMapper.countByWrapper(businessWrapper,"business_opportunity");
//        线索数量
        Integer cluesNum = indexMapper.countClues(beginCreateTime, endCreateTime);
//        商机数量
        Integer countBusiness=  indexMapper.countBusiness(beginCreateTime, endCreateTime);
//        客户数量
        Integer countCustomer=  indexMapper.countCustomer(beginCreateTime, endCreateTime);
//        销售金额
        BigDecimal salesAmount=  indexMapper.countSalesAmount(beginCreateTime, endCreateTime);
//        今日新增商机
        Integer todayBusinessNum=  indexMapper.countTodayBusiness(beginCreateTime, endCreateTime);
//        今日新增客户
        Integer todayCluesNum=  indexMapper.countTodayClues(beginCreateTime, endCreateTime);
//        今日销售额
        BigDecimal todaySalesAmount=  indexMapper.countTodaySalesAmount(beginCreateTime, endCreateTime);

        BackLogVo backLogVo = new BackLogVo();
        backLogVo.setCluesNum(cluesNum);
        backLogVo.setBusinessNum(businessNum);
        backLogVo.setContractNum(countCustomer);
        backLogVo.setSalesAmount(salesAmount);
        backLogVo.setTodayCluesNum(todayCluesNum);
        backLogVo.setTodayBusinessNum(todayBusinessNum);
        backLogVo.setTodayContractNum(todayCluesNum);
        if(cluesNum!=null){
            backLogVo.setTodaySalesAmount(todaySalesAmount);
        }
        backLogVo.setToallocatedCluesNum(clueNum);
        backLogVo.setToallocatedBusinessNum(businessNum);
        backLogVo.setTofollowedCluesNum(clueNumTo);
        backLogVo.setTofollowedBusinessNum(businessNumTo);
        System.out.println("======================================");
        System.out.println(backLogVo);
        System.out.println("======================================");
        return backLogVo;
    }
}
